﻿using System;

namespace Glue.Internals
{
    internal class MemberContainer
    {
        private readonly IHashSet<String> leftTypes;
        private readonly IHashSet<String> rightTypes;


        private MemberContainer(IHashSet<String> leftTypes, IHashSet<String> rightTypes)
        {
            this.leftTypes = leftTypes;
            this.rightTypes = rightTypes;
        }

        public static MemberContainer CreateContainer()
        {
            #if SILVERLIGHT
            return new PropertyContainer(new HashSetSilverlight<string>(), new HashSetSilverlight<string>());   
#else
            return new MemberContainer(new HashSetImpl<string>(), new HashSetImpl<string>());
            #endif
        }

        public void AddToLeftSide(String memberName)
        {
            Add(memberName, leftTypes);
        }

        public void AddToLeftSide(IObjectMember member)
        {
            Add(member,leftTypes);
        }

        public void AddToRightSide(IObjectMember member)
        {
            Add(member, rightTypes);
        }

        public void AddToRightSide(string memberName)
        {
            Add(memberName,rightTypes);
        }

        private void Add(IObjectMember member, IHashSet<String> dictionary)
        {
            var key = GetKey(member);
            Add(key,dictionary);
        }

        private void Add(string key, IHashSet<string> dictionary)
        {
            if (dictionary.Contains(key))
                dictionary.Remove(key);
            dictionary.Add(key);
        }


        public bool ContainsRightSide(IObjectMember member)
        {
            return rightTypes.Contains(GetKey(member));
        }

        public bool ContainsLeftSide(IObjectMember member)
        {
            return leftTypes.Contains(GetKey(member));
        }

        private string GetKey(IObjectMember member)
        {
            return member.Name;
        }

        public void RemoveFromLeftSide(IObjectMember member)
        {
            leftTypes.Remove(GetKey(member));
        }

        public void RemoveFromRightSide(IObjectMember member)
        {
            rightTypes.Remove(GetKey(member));
        }
    }
}
